//
// Created by 李光辉 on 2020/12/31.
//

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

const int PACK_BUF_SZ=100;
int main(void)
{
    char  pack_buf[PACK_BUF_SZ];
    int position = 0;
    int i,j;
    int comm_sz;
    int my_rank;

    double a;
    double b;
    int n;

    MPI_Init(NULL,NULL);
    MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);

    
    if(my_rank == 0)
    {
        a=1.0;
        b=2.0;
        n=3;

        MPI_Pack(&a, 1, MPI_DOUBLE, pack_buf, PACK_BUF_SZ, &position, MPI_COMM_WORLD);
        MPI_Pack(&b, 1, MPI_DOUBLE, pack_buf, PACK_BUF_SZ, &position, MPI_COMM_WORLD);
        MPI_Pack(&n, 1, MPI_INT, pack_buf, PACK_BUF_SZ, &position, MPI_COMM_WORLD);
    }

    MPI_Bcast(pack_buf, PACK_BUF_SZ, MPI_PACKED, 0, MPI_COMM_WORLD);
    if (my_rank > 0)
    {
        MPI_Unpack(pack_buf, PACK_BUF_SZ, &position, &a, 1, MPI_DOUBLE,MPI_COMM_WORLD);
        MPI_Unpack(pack_buf, PACK_BUF_SZ, &position, &b, 1, MPI_DOUBLE,MPI_COMM_WORLD);
        MPI_Unpack(pack_buf, PACK_BUF_SZ, &position, &n, 1, MPI_INT,MPI_COMM_WORLD);
    }
    printf("Proc %d > a = %.2f, b = %.2f, n = %d\n", my_rank, a, b, n);

    MPI_Finalize();
    return 0;
}
